package com.coolbi.security.web.action;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.coolbi.common.util.JsonUtil;
import com.coolbi.common.util.Md5Util;
import com.coolbi.security.biz.UserBiz;
import com.coolbi.security.biz.UserGroupBiz;
import com.coolbi.security.biz.UserToGroupBiz;
import com.coolbi.security.constant.LoginConstant;
import com.coolbi.security.entity.User;
import com.coolbi.security.entity.UserGroup;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

public class UserAction extends SessionAction {

	private static final long serialVersionUID = 1L;

	private User user;
	private List<User> userList;
	@Autowired
	private UserToGroupBiz userToGroupBiz;
	private List<UserGroup> userGroupList;
	private List<UserGroup> groupList;
	@Autowired
	private UserGroupBiz userGroupBiz;
	private UserGroup userGroup;
	@Autowired
	private UserBiz userBiz;
	private String result;

	public void setUserBiz(UserBiz userBiz) {
		this.userBiz = userBiz;
	}

	private String checkVal;

	/** 登录成功后的处理 */
	public String loginSuccess() throws Exception {
		user = getCurrentUser();
		user = userBiz.get(user.getId());
		user.setLoginStatus(LoginConstant.ON_LINE);
		user.setLoginCount(user.getLoginCount() + 1);
		user.setLoginFailedCount(LoginConstant.RETAIN_LOGIN_FAILURE_COUNT);
		user.setLoginDate(new Date());
		userBiz.update(user);
		return SUCCESS;
	}

	/** 登出成功后的处理 */
	public String logoutSuccess() throws Exception {
		user = getCurrentUser();
		user = userBiz.get(user.getId());
		user.setLoginStatus(LoginConstant.OFF_LINE);
		user.setLastLogoutDate(new Date());
		user.setShiftDate(new Date());
		userBiz.update(user);
		removeCurrentUser();
		return SUCCESS;
	}

	/** 管理员添加 */
	@Validations(requiredStrings = {
			@RequiredStringValidator(fieldName = "user.username", message = "登录名称必填"),
			@RequiredStringValidator(fieldName = "user.password", message = "登录密码必填"),
			@RequiredStringValidator(fieldName = "user.realName", message = "真实姓名必填") })
	public String addUser() throws Exception {
		try {
			/*
			 * User currentUser = getCurrentUser(); if (0 ==
			 * user.getMerchantId()) {
			 * user.setMerchantId(currentUser.getMerchantId()); }
			 * userBiz.saveByBatch(user, checkVal);
			 */
			userBiz.save(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/** 锁定管理员 */
	public String lockedUser() throws Exception {
		userBiz.lockedUser(user.getId(), LoginConstant.IS_LOCK);
		return SUCCESS;
	}

	/** 解锁管理员 */
	public String unLockUser() throws Exception {
		userBiz.lockedUser(user.getId(), LoginConstant.IS_UNLOCK);
		return SUCCESS;
	}

	/** 修改管理员密码 */
	public String updatePwd() throws Exception {
		if (userBiz.updatePassword(Md5Util.getMD5Str(user.getPassword()),
				user.getId())) {
			return SUCCESS;
		}
		return ERROR;
	}

	/**
	 * 获取所有用户组并根据用户组ID查询用户组信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findUserGroup() throws Exception {
		userGroupList = userGroupBiz.list();
		userGroup = userGroupBiz.get(userGroup.getId());
		return SUCCESS;
	}
	
	/**
	 * 获取所有管理员登录名称和ID,返回Json数据
	 * @return
	 * @throws Exception
	 */
	public String findAllUserNames() throws Exception {
		userList = userBiz.findAllUserNames();
		String[] fields = {"username", "id"};
		String json = JsonUtil.toJsonByArray(userList, fields);
		return ajax(json);
	}

	/** 修改管理员信息 */
	@Validations(requiredStrings = { @RequiredStringValidator(fieldName = "user.realName", message = "真实姓名必填") })
	public String updateUser() {
		try {
			userBiz.updateByBatch(user, checkVal);
		} catch (Exception e) {
			e.printStackTrace();
			addActionError(e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}

	/** 根据ID获取管理员及所属用户组、所有用户组信息 */
	public String findUserById() throws Exception {
		user = userBiz.get(user.getId());
		groupList = userToGroupBiz.findGroupByUserId(user.getId());
		userGroupList = userGroupBiz.list();
		return SUCCESS;
	}

	/** 根据ID获取管理员信息 */
	public String showUserById() throws Exception {
		user = userBiz.get(user.getId());
		groupList = userToGroupBiz.findGroupByUserId(user.getId());
		return SUCCESS;
	}

	/** 分页查看管理员 */
	public String listByPage() throws Exception {
		pager.setRecordCount(userBiz.count());
		userList = userBiz.list(
				(pager.getCurrPageNo() - 1) * pager.getPageSize(),
				pager.getPageSize());
		return SUCCESS;
	}

	/** 检测email是否存在 */
	public String checkEmail() throws Exception {
		try {
			boolean flag = false;
			if (null == user.getId()) {
				flag = userBiz.existByEmail(user.getEmail());
			}
			return ajax(String.valueOf(!flag));
		} catch (Exception e) {
			e.printStackTrace();
			return ajax("error");
		}
	}

	/** 检测手机号码是否存在 */
	public String checkTel() {
		try {
			boolean flag = false;
			if (null == user.getId()) {
				flag = userBiz.existByTel(user.getTelephone());
			}
			return ajax(String.valueOf(!flag));
		} catch (Exception e) {
			e.printStackTrace();
			return ajax("error");
		}
	}

	/** 检测用户名是否存在 */
	public String checkUserName() throws Exception {
		try {
			boolean flag = false;
			if (null == user.getId()) {
				flag = userBiz.existByUserName(user.getUsername());
			}
			return ajax(String.valueOf(!flag));
		} catch (Exception e) {
			e.printStackTrace();
			return ajax("error");
		}
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public List<UserGroup> getUserGroupList() {
		return userGroupList;
	}

	public void setUserGroupList(List<UserGroup> userGroupList) {
		this.userGroupList = userGroupList;
	}

	public void setUserToGroupBiz(UserToGroupBiz userToGroupBiz) {
		this.userToGroupBiz = userToGroupBiz;
	}

	public void setUserGroupBiz(UserGroupBiz userGroupBiz) {
		this.userGroupBiz = userGroupBiz;
	}

	public String getCheckVal() {
		return checkVal;
	}

	public void setCheckVal(String checkVal) {
		this.checkVal = checkVal;
	}

	public List<UserGroup> getGroupList() {
		return groupList;
	}

	public void setGroupList(List<UserGroup> groupList) {
		this.groupList = groupList;
	}

	public UserGroup getUserGroup() {
		return userGroup;
	}

	public void setUserGroup(UserGroup userGroup) {
		this.userGroup = userGroup;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

}
